package com.jiangman.weather.adapter;

/**
 * Created by secoro on 2017/4/26.
 */

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiangman.weather.R;
import com.jiangman.weather.db.dao.WeatherDao;
import com.jiangman.weather.view.CircleTempView;

import java.util.ArrayList;

/**
 * 自定义的listview适配器
 * 抽屉菜单的listview适配器
 */
public class MyLocationBaseAdapter extends BaseAdapter {
    Activity mActivity;
    private ArrayList<String> mAreasNameList;
    /**
     * 用于存放所有已选择的城市的实时天气的集合和areasNameList的位置一一对应
     */
    private ArrayList<String> mAreaRTTempList;
    /**
     * 存放左侧抽屉不同temp的icon集合
     */
    private ArrayList<Integer> mTempIconList;
    private WeatherDao dao;

    public MyLocationBaseAdapter(Activity mActivity, ArrayList<String> mAreasNameList, ArrayList<String> mAreaRTTempList, ArrayList<Integer> mTempIconList, WeatherDao dao) {
        this.mActivity = mActivity;
        this.mAreasNameList = mAreasNameList;
        this.mAreaRTTempList = mAreaRTTempList;
        this.mTempIconList = mTempIconList;
        this.dao = dao;
    }

    @Override
    public int getCount() {
        return mAreasNameList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        LocationViewHolder locationViewHolder = new LocationViewHolder();
        if (convertView != null) {
            view = convertView;
            locationViewHolder = (LocationViewHolder) view.getTag();
        } else {
            view = View.inflate(mActivity, R.layout.item_drawer_location, null);
            locationViewHolder.tv_title = (TextView) view.findViewById(R.id.tv_title);
            locationViewHolder.iv_yes = (ImageView) view.findViewById(R.id.iv_yes);
            locationViewHolder.circleTempView = (CircleTempView) view.findViewById(R.id.circle_temp_view);
            view.setTag(locationViewHolder);
        }
        locationViewHolder.circleTempView.setTempValue(mAreaRTTempList.get(position) + "°");
        locationViewHolder.circleTempView.setTempIcon(mTempIconList.get(position));
        String name = mAreasNameList.get(position);
        locationViewHolder.tv_title.setText(name);
        if (dao.getMainAreaName().equals(name)) {
            locationViewHolder.iv_yes.setImageResource(R.mipmap.ic_yes);
            locationViewHolder.iv_yes.setVisibility(View.VISIBLE);
        } else {
            locationViewHolder.iv_yes.setVisibility(View.INVISIBLE);
        }
        return view;
    }


    static class LocationViewHolder {
        TextView tv_title;
        ImageView iv_yes;
        CircleTempView circleTempView;
    }
}
